//
//  XMLExampleViewController.m
//  XMLExample
//

#import "XMLExampleViewController.h"

@implementation XMLExampleViewController

- (void)parser:(NSXMLParser *)parser validationErrorOccured:(NSError *)err {
	UIAlertView *myAlert = [[UIAlertView alloc]
							initWithTitle:@"Validation Error"
							message:err.localizedDescription
							delegate:nil
							cancelButtonTitle:@"OK"
							otherButtonTitles:nil];
	[myAlert show];
	[myAlert release];
}

- (void)parser:(NSXMLParser *)parser parseErrorOccured:(NSError *)err {
	UIAlertView *myAlert = [[UIAlertView alloc]
							initWithTitle:@"Fatal Error"
							message:err.localizedDescription
							delegate:nil
							cancelButtonTitle:@"OK"
							otherButtonTitles:nil];
	[myAlert show];
	[myAlert release];
}

- (void)parserDidStartDocument:(NSXMLParser *)parser {
	[articles removeAllObjects];
}

- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qualifiedName
	attributes:(NSDictionary *)attributeDict {

	currentElement = elementName;
	
	if ([elementName isEqualToString:@"item"]) {
		itemActive = YES;
		currentTitle = [[NSMutableString alloc] init];
		currentGuid = [[NSMutableString alloc] init];
		pubDate = [[NSMutableString alloc] init];
	}
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
	if (itemActive) {
		NSString *fixedString = [string stringByTrimmingCharactersInSet:
								 [NSCharacterSet whitespaceAndNewlineCharacterSet]];
		if ([currentElement isEqualToString:@"title"]) {
			[currentTitle appendString:fixedString];
		}
		if ([currentElement isEqualToString:@"guid"]) {
			[currentGuid appendString:fixedString];
		}
		if ([currentElement isEqualToString:@"pubDate"]) {
			[pubDate appendString:fixedString];
		}
	}
}

- (void)parser:(NSXMLParser *)parser
 didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qName {

	if ([elementName isEqualToString:@"item"]) {
		NSDictionary *record = [NSDictionary dictionaryWithObjectsAndKeys:
								currentTitle, @"articleTitle",
								currentGuid, @"articleURL",
								pubDate, @"publicationDate",
								nil];
		[articles addObject:record];
		
		[currentTitle release];
		[currentGuid release];
		[pubDate release];
		
		itemActive = NO;
	}
}

- (void)parserDidEndDocument:(NSXMLParser *)parser {
	NSLog(@"%@", articles);
}

- (void)retrieveXML:(id)sender {
	[parser parse];
}

- (void)createUI {
	
	UIButton *btnRetrieveXML = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	btnRetrieveXML.frame = CGRectMake(100, 200, 120, 34);
	[btnRetrieveXML setTitle:@"Retrieve XML" forState:UIControlStateNormal];
	[btnRetrieveXML addTarget:self action:@selector(retrieveXML:)
			 forControlEvents:UIControlEventTouchUpInside];
	[self.view addSubview:btnRetrieveXML];
}

- (void)viewDidLoad {
    [super viewDidLoad];
	
	articles = [[NSMutableArray alloc] init];
	
	NSURL *url = [NSURL URLWithString:@"http://rss.cnn.com/rss/edition.rss"];
	parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
	parser.delegate = self;
	
	[self createUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)dealloc {
	
	[articles release];
	[parser release];
	
    [super dealloc];
}

@end
